import unittest
import logging 
from line_algo import *
from shape import *
import house
from math import pi
class test_line_algo(unittest.TestCase):
    def setup(self):
        print 'setting up'
    def test_nodes_equal(self):
        print("Test0")
        assert(nodes_equal((0,0), (0,0)) and nodes_equal((0,0), (1e-10, -1e-10)))
    def test_points_equal(self):
        print("Test1")
        assert(point(1, 1) == point(1, 1) and point(1 + 1e-10, 1) == point(1, 1 + 1e-10))
    def test_flip_point(self):
        assert(flip_point((1, 1)) == 1, -1)

    def test_match(self):
        print("Test2")
        assert match((1, 0), (1, 0), (0, 0), (1, 1), (1, 1))
        assert match((0, 0), (1, 1), (0, 0), (1, 1), (0, 1))
        assert match((0, 1), (0, 0), (0, 0), (0, 1), (0, 0))
        assert match((1 - 1e-10, 1 + 1e-10), (0, 0), (0, 0), (0, 0), (1, 1))
    def test_lineIntersection(self):
        point, type = lineIntersection((0, 4), (3, 4.1), (0, 0), (0, 4)) 
        assert(point == (0, 4))
        assert(type == {'endpoint intersection': True, 'intersection': True})
        seg2 = ((24.142135623730951, 23.535533905932738), (24.142135623730947, 9.3933982822017867))
        seg1 = ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378))
        point, type = lineIntersection(seg1[0], seg1[1], seg2[0], seg2[1])
        assert(point == None, None)
        assert('overlapped intersection' in type)
        seg1, seg2 = seg2, seg1
        point, type = lineIntersection(seg1[0], seg1[1], seg2[0], seg2[1])
        assert(point == None, None)
        assert('overlapped intersection' in type)

    def test_clamp_segs(self):
        assert((0, 1), (0,2), (0, 0), (1, 0) == clamp_segs((0, 1), (0, 2), (0, 0), (1, 0)))
        assert((0, 1), (0, 2), (0, -1), (0, 0) == clamp_segs((0, 1), (0, 2), (0, -1), (EPSILON/2, 0)))
        seg1 = ((14.142135623730951, 13.535533905932738), (24.142135834465194, 3.535534116666982))
        seg2 = ((14.142135623730951, 13.535533905932738), (24.142135623730951, 3.5355339059327378))
        (n1, n2, n3, n4) = clamp_segs(seg1[0], seg1[1], seg2[0], seg2[1])
        assert(abs(n1[0] - seg1[0][0]) < EPSILON/10000.0 and abs(n1[1] - seg1[0][1]) < EPSILON/10000.0)
        assert(abs(n2[0] - seg1[1][0]) < EPSILON/10000.0 and abs(n2[1] - seg1[1][1]) < EPSILON/10000.0)
        assert(abs(n3[0] - n1[0]) < EPSILON/10000.0 and abs(n3[1] - n1[1]) < EPSILON/10000.0)
        assert(abs(n4[0] - n2[0]) < EPSILON/10000.0 and abs(n4[1] - n2[1]) < EPSILON/10000.0)

    def test_segments_equal(self):
        print("Test3")
        seg1 = ((0, 0), (1, 1))
        seg2 = ((0, 0), (1, 1))
        seg3 = ((1e-10, -1e-10), (1+1e-10, 1-1e-10))
        seg4 = ((1, 1), (0, 0))
        seg5 = ((1+1e-10, 1-1e-10), (1e-10, -1e-10))
        seg6 = ((0, 0), (0, 1))
        assert(segments_equal(seg1, seg2) and segments_equal(seg1, seg3) and segments_equal(seg1, seg4) and segments_equal(seg5, seg1) and not segments_equal(seg4, seg6))
    def test_interpolate(self):
        print("Test4")
        assert(nodes_equal((0.5, 0.5), interpolate(((0, 0), (1, 1)), 0.5)))
    def test_split_overlapped_vertical_line_segments(self):
        print("Test5")
        seg1 = ((0, 0), (0, 1))
        seg2 = ((0, -1), (0, 2))
        seg3 = ((0, -1), (0, 0))
        seg4 = ((0, 1), (0, 3))
        seg5 = ((0, 2), (0, 3))
        seg6 = ((4.0, 4.0), (4.0, 8.0))
        seg7 = ((4, 8), (4, 4))
        seg8 = ((4, 0), (4, 8))

        split_seg6, split_seg7 = split_overlapped_vertical_line_segments(seg6, seg7)
        assert(segments_equal(split_seg6['overlap'], seg6) and segments_equal(split_seg7['overlap'], seg7))

        split_seg7, split_seg8 = split_overlapped_vertical_line_segments(seg7, seg8)
        assert(split_seg7['overlap'] == ((4, 4), (4, 8)) and split_seg8['before_overlap'] == ((4, 0), (4, 4)))

        split_seg1, split_seg_1 = split_overlapped_vertical_line_segments(seg1, seg1)
        assert(split_seg1['before_overlap'] == (None, None) and split_seg1['overlap'] == seg1 and split_seg1['after_overlap'] == (None, None))
        assert(split_seg_1['before_overlap'] == (None, None) and split_seg_1['overlap'] == seg1 and split_seg_1['after_overlap'] == (None, None))

        split_seg1, split_seg2 = split_overlapped_vertical_line_segments(seg1, seg2)
        assert(split_seg1['before_overlap'] == (None, None) and split_seg1['after_overlap'] == (None, None))
        assert(split_seg2['before_overlap'] == ((0, -1), (0,0)) and split_seg2['overlap'] == ((0, 0), (0, 1)) and split_seg2['after_overlap'] == ((0, 1), (0, 2)))

        split_seg1, split_seg2 = split_overlapped_vertical_line_segments((seg1[1], seg1[0]), (seg2[1], seg2[0]))
        assert(split_seg1['before_overlap'] == (None, None) and split_seg1['after_overlap'] == (None, None))
        assert(split_seg2['before_overlap'] == ((0, -1), (0,0)) and split_seg2['overlap'] == ((0, 0), (0, 1)) and split_seg2['after_overlap'] == ((0, 1), (0, 2)))


        split_seg2, split_seg3 = split_overlapped_vertical_line_segments(seg2, seg3)
        assert(split_seg2['before_overlap'] == (None, None) and split_seg2['overlap'] == ((0, -1), (0, 0)) and split_seg2['after_overlap'] == ((0, 0), (0, 2)))
        assert(split_seg3['before_overlap'] == (None, None) and split_seg3['overlap'] == seg3 and split_seg3['after_overlap'] == (None, None))
    
        split_seg2, split_seg4 = split_overlapped_vertical_line_segments(seg2, seg4)
        assert(split_seg2['before_overlap'] == ((0, -1), (0, 1)) and split_seg2['overlap'] == ((0, 1), (0, 2)) and split_seg2['after_overlap'] == (None, None))
        assert(split_seg4['before_overlap'] == (None, None) and split_seg4['overlap'] == split_seg2['overlap'] and split_seg4['after_overlap'] == ((0, 2), (0, 3)))

        split_seg1, split_seg4 = split_overlapped_vertical_line_segments(seg1, seg4)
        assert(split_seg1['before_overlap'] == seg1 and split_seg1['overlap'] == (None, None) and split_seg1['after_overlap'] == (None, None))
        assert(split_seg4['before_overlap'] == seg4 and split_seg4['overlap'] == split_seg1['overlap'] and split_seg4['after_overlap'] == (None, None))

        split_seg4, split_seg5 = split_overlapped_vertical_line_segments(seg4, seg5)
        assert(split_seg4['before_overlap'] == ((0, 1), (0, 2)) and split_seg4['overlap'] == ((0, 2), (0, 3)) and split_seg4['after_overlap'] == (None, None))
        assert(split_seg5['before_overlap'] == (None, None) and split_seg5['overlap'] == ((0, 2), (0, 3)) and split_seg5['after_overlap'] == (None, None))

        split_seg4, split_seg5 = split_overlapped_vertical_line_segments(seg4, seg5)
        assert(split_seg4['before_overlap'] == ((0, 1), (0, 2)) and split_seg4['overlap'] == ((0, 2), (0, 3)) and split_seg4['after_overlap'] == (None, None))
        assert(split_seg5['before_overlap'] == (None, None) and split_seg5['overlap'] == ((0, 2), (0, 3)) and split_seg5['after_overlap'] == (None, None))

        split_seg5, split_seg4 = split_overlapped_vertical_line_segments(seg5, seg4)
        assert(split_seg4['before_overlap'] == ((0, 1), (0, 2)) and split_seg4['overlap'] == ((0, 2), (0, 3)) and split_seg4['after_overlap'] == (None, None))
        assert(split_seg5['before_overlap'] == (None, None) and split_seg5['overlap'] == ((0, 2), (0, 3)) and split_seg5['after_overlap'] == (None, None))
        
        seg6 = ((24.142135623730951, 23.535533905932738), (24.142135623730947, 9.3933982822017867))
        seg7 = ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378))

        split_seg1, split_seg2 = split_overlapped_vertical_line_segments(seg1, seg2)

        try:
            seg1 = ((0, 1), (1, 2))
            s1, s2 = split_overlapped_vertical_line_segments(seg1, seg1)
        except ValueError, val:
            assert(str(val) == 'not vertical')
        try:
            seg1 = ((0, 1), (1, 2))
            split_overlapped_vertical_line_segments(((0, 1), (0, 2)), ((1, 1), (1, 2)))
        except ValueError, val:
           assert(str(val) == 'not parallel')
           
    def test_split_overlapped_horizontal_line_segments(self):
        print("Test6")
        seg1 = ((0, 0), (1, 0))
        seg2 = ((-1, 0), (2, 0))
        seg3 = ((-1, 0), (0, 0))
        seg4 = ((1, 0), (3, 0))
        seg5 = ((2, 0), (3, 0))

        split_seg1, split_seg2 = split_overlapped_horizontal_line_segments(seg1, seg2)
        assert(split_seg1['before_overlap'] == (None, None) and split_seg1['overlap'] == seg1 and split_seg1['after_overlap'] == (None, None))
        assert(split_seg2['before_overlap'] == ((-1, 0), (0, 0)) and split_seg2['overlap'] == seg1 and split_seg2['after_overlap'] == ((1, 0), (2, 0)))
    def test_split_overlapped_parallel_line_segments(self):
        print("Test7")
        seg1 = ((0, 0), (1, 1))
        seg2 = ((-1, -1), (2, 2))
        split_seg1, split_seg2 = split_overlapped_parallel_line_segments(seg1, seg2)
        assert(split_seg1['before_overlap'] == (None, None) and split_seg1['overlap'] == seg1 and split_seg1['after_overlap'] == (None, None))
        assert(split_seg2['before_overlap'] == ((-1, -1), (0, 0)) and split_seg2['overlap'] == seg1 and split_seg2['after_overlap'] == ((1, 1), (2, 2)))
    def test_inside_shape(self):
        print("Test8")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        assert(inside_shape((1, 1), tri))
        assert(inside_shape((3, 3), squ))
        assert(inside_shape((4, 3), shape1))
        assert(not inside_shape((4, 4), shape1))
        assert(inside_shape((3, 5),shape1))
        tri.translate((4, 0))
        assert(not inside_shape((2, 4), tri))
        squ = shape('square.txt')
        assert(not inside_shape((0, 5), squ))

        s = shape('', '', [((24.142135623730979, 10.606601717798192), (17.071067811865468, 17.677669529663696)), ((17.071067811865468, 17.677669529663696), (17.071067811865429, 10.606601717798256)), ((14.142135623730951, 13.535533905932738), (17.071067811865429, 10.606601717798263)), ((7.0710678118654755, 14.142135623730951), (14.142135623730951, 14.142135623730951)), ((14.142135623730951, 13.535533905932738), (14.142135623730951, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654746, 7.0710678118654755)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 10.60660171779822))])
        
        assert(not inside_shape((20.60660171779816352000, 10.60660171779819194171), s))
    def test_on_boundary(self):
        print("Test9")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        assert(on_boundary((0, 0), tri) and on_boundary((4, 4), squ) and on_boundary((0, 4), shape1) and not on_boundary((4, 3), shape1) and on_boundary((4, 4), shape1))
    def test_segment_contained_in(self):
        print("Test10")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        for s in [tri, squ, shape1]:
            for seg in s.line_segments:
                assert(segment_contained_in(seg, s))
        assert(segment_contained_in(((0, 0), (1, 1)), tri))
        assert(segment_contained_in(((0, 4), (4, 4)), squ))
        assert(segment_contained_in(((4, 0), (4, 4)), shape1))
        assert(not segment_contained_in(((0, 0), (7, 3)), shape1))
        assert(not segment_contained_in(((0, 0), (0, 5)), shape1))
        assert(segment_contained_in(((4, 4), (8, 0)), shape1))
        assert(segment_contained_in(((3, 5), (4, 4)), shape1))
        assert(segment_contained_in(((4, 0), (0, 0)), shape1))
        s = shape('', '', [((17.071067811865479, 17.07106781186549), (17.071067811865476, 7.0710678118654755)), ((12.071067811865479, 12.07106781186549), (12.071067811865479, 22.071067811865461)), ((12.071067811865479, 22.071067811865461), (17.071067811865479, 17.07106781186549)), ((7.0710678118654755, 7.0710678118654755), (12.071067811865476, 12.071067811865476)), ((17.071067811865476, 7.0710678118654755), (12.071067811865476, 2.0710678118654755)), ((12.071067811865476, 2.0710678118654755), (14.142135623730951, 0.0)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654755, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 0.0))])
        assert(not segment_contained_in(((12.071067811865476, 2.0710678118654755), (12.07106781186547, 22.071067811865476)), s))    

        s = shape('', '', [((7.0710678118654755, 7.0710678118654755), (12.071067811865476, 12.071067811865476)), ((12.071067811865476, 12.071067811865476), (17.071067811865476, 7.0710678118654755)), ((17.071067811865476, 7.0710678118654755), (12.071067811865476, 2.0710678118654755)), ((12.071067811865476, 2.0710678118654755), (14.142135623730951, 0.0)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654755, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 0.0))])
        seg = ((12.071068711865476, 2.0710678118654764), (2.0710678118654737, 12.071067811865476))
        assert(not segment_contained_in(seg, s))

                  
        s = shape('', '', [((24.142135623730951, 3.5355339059327378), (14.142135623730951, 13.535533905932738)), ((14.142135623730951, 13.535533905932738), (24.142135623730951, 23.535533905932738)), ((7.0710678118654755, 14.142135623730951), (14.142135623730951, 14.142135623730951)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654746, 7.0710678118654746)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865479)), ((14.142135623730951, 27.071067811865479), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378))])
        assert(segment_contained_in(((14.142135623730951, 13.535533905932738), (24.142135834465194, 3.535534116666982)), s))

        
        seg =  ((24.142135623730979, 10.60660171779819), (17.071067811865344, 10.606601717798194))
        s = shape('', '', [((24.142135623730979, 10.606601717798192), (17.071067811865468, 17.677669529663696)), ((17.071067811865468, 17.677669529663696), (17.071067811865429, 10.606601717798256)), ((14.142135623730951, 13.535533905932738), (17.071067811865429, 10.606601717798263)), ((7.0710678118654755, 14.142135623730951), (14.142135623730951, 14.142135623730951)), ((14.142135623730951, 13.535533905932738), (14.142135623730951, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654746, 7.0710678118654755)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 10.60660171779822))])
        assert(not segment_contained_in(seg, s))

    def test_segment_inside(self):
        print("Test11")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        for s in [tri, squ, shape1]:
            for seg in s.line_segments:
                assert(not segment_inside(seg, s))
        assert(segment_inside(((0, 0), (1, 1)), tri))
        assert(segment_inside(((4, 4), (3, 0)), squ))
        assert(segment_inside(((4, 4), (4, 0)), shape1))
        assert(not segment_contained_in(((0, 0), (7, 3)), shape1))
        assert(not segment_contained_in(((0, 0), (0, 5)), shape1))
        assert(not segment_inside(((4, 0), (4, 8)), shape1))
        assert(segment_inside(((0, 4), (4, 4)), shape1))
        assert(segment_inside(((4, 8), (3, 1)), shape1))
        assert(segment_inside(((4, 0), (4, 4)), shape1))
        tri.translate((4, 0))
        assert(not segment_inside(((0, 4), (4, 4)), tri))
    def test_segment_on_boundary(self):
        print("Test12")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        for s in [tri, squ, shape1]:
            for seg in s.line_segments:
                assert(segment_on_boundary(seg, s))
        assert(not segment_on_boundary(((0, 0),(1, 1)), tri))
        assert(not segment_on_boundary(((0, 0), (3, 3)), squ))
        assert(not segment_on_boundary(((3, 5), (8, 0)), shape1))
        tri.translate((4, 0))
        assert(not segment_on_boundary(((0, 4), (4, 4)), tri))
    def test_segment_outside(self):
        print("Test13")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        for s in [tri, squ, shape1]:
            for seg in s.line_segments:
                assert(not segment_outside(seg, s))
        
        assert(segment_outside(((4, 0),(3, 1.1)), tri))
        assert(segment_outside(((0, 4), (3, 4.1)), squ))
        assert(not segment_outside(((3, 5), (8, 0)), shape1))
        assert(segment_outside(((0, 4), (0, 8)), shape1))
        assert(segment_outside(((4, 0), (8, 0)), squ))
        tri.translate((4, 0))
        assert(segment_outside(((0, 4), (4, 4)), tri))
        squ = shape('square.txt')
        #logging.on('segment_outside', True)
        assert(segment_outside(((0, 0), (0, 10)), squ))
        #logging.on('segment_outside', False)
    def test_split_segment(self):
        print("Test14")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        for s in [tri, squ, shape1]:
            for seg in s.line_segments:
                assert(split_segment(seg, s)['inside'] == [] and split_segment(seg, s)['outside'] == [] and segments_equal(split_segment(seg, s)['boundary'][0], seg))
        seg1 = ((4, 0), (4, 8))
        split_seg1 = split_segment(seg1, shape1)
        assert(segments_equal(split_seg1['inside'][0], ((4, 0), (4, 4))) and segments_equal(split_seg1['boundary'][0], ((4, 4), (4, 8))))


        seg2 = ((0, 0), (0, 8))
        split_seg2 = split_segment(seg2, shape1)
        assert(split_seg2['inside'] == [] and segments_equal(split_seg2['outside'][0], ((0, 4), (0, 8))))

        seg3 = ((1, 1) , (8, 1))
        split_seg3 = split_segment(seg3, shape1)
        assert(split_seg3['outside'] != [])

        seg4 = ((3, 5), (8, 0))
        split_seg4 = split_segment(seg4, shape1)
        assert(segments_equal(split_seg4['inside'][0], ((3, 5), (4, 4))) and split_seg4['outside'] == [] and segments_equal(split_seg4['boundary'][0], ((4, 4), (8, 0))))

        

        s1 = shape('', '', [((7.0710678118654755, 7.0710678118654755), (12.071067811865476, 12.071067811865476)), ((12.071067811865476, 12.071067811865476), (17.071067811865476, 7.0710678118654755)), ((17.071067811865476, 7.0710678118654755), (12.071067811865476, 2.0710678118654755)), ((12.071067811865476, 2.0710678118654755), (14.142135623730951, 0.0)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654755, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 0.0))])
        segs = split_segment(((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378)), s1)
        assert(segs['inside'] == [])
        assert(segs['outside'] == [])
        assert(len(segs['boundary']) == 1)
        assert(segs_equal(segs['boundary'][0], ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378))))
        
class test_geometry(unittest.TestCase):
    def test_mag(self):
        print("Test15")
        assert(float_equal(mag((1, 1)), pow(2, 1/2.0)))
        assert(mag((1, 0)) == 1)
        assert(mag((-1, 0)) == 1)
    def test_dot(self):
        print("Test16")
        assert(dot((1, 0), (1, 0)) == 1)
        assert(dot((1, 0), (0, 1)) == 0)
        assert(dot((1, 0), (-1, 0)) == -1)
    def test_angle(self):
        print("Test17")
        assert(angle((1, 0)) == 0)
        assert(angle((0, 1)) == pi/2.0)

    def test_quadrant(self):
        assert(quadrant((1, 0)) == 0)
        assert(quadrant((1, 1)) == 0)
        assert(quadrant((0, 1)) == 0)
        assert(quadrant((-1, 1)) == 1)
        assert(quadrant((-1, 0)) == 1)
        assert(quadrant((-1, -1)) == 2)
        assert(quadrant((0, -1)) == 2)
        assert(quadrant((1, -1)) == 3)

    def test_vec1_before_vec2(self):
        vecs = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (0, -1), (1, -1)]
        for i in range(len(vecs)):
            vecs[i] = normalize(vecs[i])
        for i in range(len(vecs)):
            if i < len(vecs) - 1:
                assert(vec1_before_vec2(vecs[i], vecs[i+1]))
            if i > 0:
                assert(not vec1_before_vec2(vecs[i], vecs[i - 1]))

    def test_fast_rotate2d(self):
        v1 = [1, 0]
        assert(nodes_equal(fast_rotate2d(v1, 0, 1), (0, 1)))
        assert(nodes_equal(fast_rotate2d(v1, 0, -1), (0, -1)))

    def test_rotate2d(self):
        print("Test18")
        assert(nodes_equal(rotate2d((1, 0), pi/2.0), (0, 1)))

#    def test_vertices(self):
        #tri = shape('unittests/triangle.txt', 'triangle')
        #squ = shape('unittests/square.txt', 'square')

    def test_subset(self):
        print("Test19")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', '')
        assert(subset(tri, squ))
        assert(subset(tri, shape1))
        squ2 = shape('square.txt', 'square')
        shape2 = shape('unittests/shape1.txt', 'shape1')
        shape2.scale(2.25, 2.25)
        assert(subset(squ2, shape2))
        s = shape1.subtract_shape(squ)
        tri.translate((4, 0))
        try:
            logging.on('subset', True)
            assert(subset(tri, s))
        finally:
            logging.on('subset', False)
        s1 = shape('', '',[((12.07106781186547, 22.071067811865476), (22.071067811865472, 12.071067811865476)), ((22.071067811865472, 12.071067811865476), (12.071067811865476, 2.0710678118654755)), ((12.071067811865476, 2.0710678118654755), (12.07106781186547, 22.071067811865476))])
        s2 = shape('', '', [((17.071067811865479, 17.07106781186549), (17.071067811865476, 7.0710678118654755)), ((12.071067811865479, 12.07106781186549), (12.071067811865479, 22.071067811865461)), ((12.071067811865479, 22.071067811865461), (17.071067811865479, 17.07106781186549)), ((7.0710678118654755, 7.0710678118654755), (12.071067811865476, 12.071067811865476)), ((17.071067811865476, 7.0710678118654755), (12.071067811865476, 2.0710678118654755)), ((12.071067811865476, 2.0710678118654755), (14.142135623730951, 0.0)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654755, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 0.0))])
        assert(not subset(s1, s2))

        s1 = shape('', '', [((22.071067811865476, 12.071067811865479), (12.071067811865476, 2.0710678118654764)), ((12.071067811865476, 2.0710678118654764), (2.0710678118654737, 12.071067811865476)), ((2.0710678118654737, 12.071067811865476), (22.071067811865476, 12.071067811865479))])

        s2 = shape('', '', [((7.0710678118654755, 7.0710678118654755), (12.071067811865476, 12.071067811865476)), ((12.071067811865476, 12.071067811865476), (17.071067811865476, 7.0710678118654755)), ((17.071067811865476, 7.0710678118654755), (12.071067811865476, 2.0710678118654755)), ((12.071067811865476, 2.0710678118654755), (14.142135623730951, 0.0)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654755, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 3.5355339059327378)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 0.0))])
        assert(not subset(s1, s2))
        
        s1 = shape('', '', [((14.142135623730949, 7.0710678118654791), (4.1421356237309546, 17.071067811865479)), ((4.1421356237309546, 17.071067811865479), (14.142135623730951, 27.071067811865479)), ((14.142135623730951, 27.071067811865479), (14.142135623730949, 7.0710678118654791))])
        s2 = house.house
        assert(subset(s1, s2))


        s1 = shape('', '', [((17.071067811865344, 10.606601717798194), (17.071067811865113, 17.677669529664232)), ((17.071067811865113, 17.677669529664232), (24.142135623730965, 17.677669529663635)), ((24.142135623730965, 17.677669529663635), (24.142135623730979, 10.60660171779819)), ((24.142135623730979, 10.60660171779819), (17.071067811865344, 10.606601717798194))])

        s2 = shape('', '', [((24.142135623730979, 10.606601717798192), (17.071067811865468, 17.677669529663696)), ((17.071067811865468, 17.677669529663696), (17.071067811865429, 10.606601717798256)), ((14.142135623730951, 13.535533905932738), (17.071067811865429, 10.606601717798263)), ((7.0710678118654755, 14.142135623730951), (14.142135623730951, 14.142135623730951)), ((14.142135623730951, 13.535533905932738), (14.142135623730951, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654746, 7.0710678118654755)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 23.535533905932738), (24.142135623730951, 10.60660171779822))])
        assert(not subset(s1, s2))

    def test_intersect(self):
        print("Test20")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        assert(intersect(tri, squ))
        assert(intersect(tri, shape1))
        assert(intersect(squ, shape1))
        tri.translate((5, 0))
        assert(intersect(tri, shape1))
        tri.translate((3, 0))
        assert(not intersect(tri, shape1))
        squ.translate((1, 1))
        assert(intersect(squ, shape1))
        squ.translate((7, 0))
        assert(not intersect(squ, shape1))
    def test_intersections(self):
        print("Test21")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        assert(intersections(tri, [squ, shape1]))
        tri.translate((8, 0))
        assert(not intersections(tri, [squ, shape1]))
    def test_subtract_shape(self):
        print("Test22")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        s = subtract_shape(tri, squ)

        assert(type(s) != type(None))
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', '')
        shape2 = shape('unittests/shape1.txt', 'shape1')
        shape2.scale(2.25, 2.25)
        s = shape1.subtract_shape(squ)
        assert(type(s) != type(None))
        tri.translate((4, 0))
        assert(subset(tri, s))

class test_shape(unittest.TestCase):
    def test_rotate(self):
        print("Test23")
        print 'testing rotate'
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        tri.rotate((0, 0), pi)
        line_segments = tri.line_segments
        assert(len(line_segments) == 3)
        assert([1 for seg in line_segments if segments_equal(((0, 0), (-4, 0)), seg)])
        assert([1 for seg in line_segments if segments_equal(seg, ((-4, 0), (0, -4)))])
        assert([1 for seg in line_segments if segments_equal(((0, -4), (0, 0)), seg)])

    def test_translate(self):
        print("Test24")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        tri.translate((1, 1))
        line_segments = tri.line_segments
        assert([1 for seg in line_segments if segments_equal(seg, ((1, 1), (1, 5)))])
        assert([1 for seg in line_segments if segments_equal(seg, ((1, 5), (5, 1)))])
        assert([1 for seg in line_segments if segments_equal(seg, ((5, 1), (1, 1)))])
        
    def test_flip(self):
        tri = shape('unittests/triangle.txt')
        tri.flip()
        assert((0, 0) in tri.points and (0, -4)  in tri.points and (4, 0) in tri.points)
        assert(((0, 0), (0, -4)) in tri.line_segments and ((0, -4), (4, 0)) in tri.line_segments and ((4, 0), (0, 0)) in tri.line_segments)
        for v in tri.vertices:
            assert(len(v[1]) == 2)
    def test_subtract(self):
        print("Test25")
        tri = shape('unittests/triangle.txt', 'triangle')
        squ = shape('unittests/square.txt', 'square')
        shape1 = shape('unittests/shape1.txt', 'shape1')
        new_squ = squ.subtract_shape(tri)
        assert(len(new_squ.line_segments) == 3)
        assert(((0, 4), (4, 4)) in new_squ.line_segments)
        assert(((4, 4), (4, 0)) in new_squ.line_segments)
        assert(((0, 4), (4, 0)) in new_squ.line_segments)
        squ = shape('square.txt', 'square')
        shape1 = shape('shape1.txt', 'shape1')
        shape1.scale(2.5, 2.5)
        assert(subset(squ, shape1))
       # print 'subtracting'
        s = shape1.subtract_shape(squ)
       #b print s
#    def test_fit_vertex(self):
        

    def testfitshape(self):
        print("Test26")
        print 'running fitshape'
        tri1 = shape('triangle.txt')
        tri2 = shape('triangle.txt')
        squ = shape('square.txt')
        shape1 = shape('shape1.txt')
        shape1.scale(2.5, 2.5)
        fitshapes([tri1, tri2, squ], shape1)
        large_tri1 = shape('shapes/large_triangle.txt')
        s = shape('', '', [((24.142135623730951, 16.464466094067262), (17.071067811865479, 16.464466094067262)), ((17.071067811865479, 16.464466094067262), (17.071067811865476, 23.535533905932738)), ((14.142135623730951, 0.0), (7.0710678118654755, 7.0710678118654755)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654755, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (17.071067811865476, 23.535533905932738)), ((24.142135623730951, 3.5355339059327378), (24.142135623730955, 16.464466094067262)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 0.0))])
        assert(fitshape(large_tri1, s).next())

    def testfitshapes2(self):
        s = shape('', '', [((24.142135623730951, 3.5355339059327378), (14.142135623730951, 13.535533905932738)), ((14.142135623730951, 13.535533905932738), (24.142135623730951, 23.535533905932738)), ((7.0710678118654755, 14.142135623730951), (14.142135623730951, 14.142135623730951)), ((14.142135623730951, 3.5355339059327378), (14.142135623730951, 14.142135623730951)), ((0.0, 14.142135623730951), (7.0710678118654746, 7.0710678118654755)), ((7.0710678118654755, 7.0710678118654755), (0.0, 7.0710678118654755)), ((0.0, 7.0710678118654755), (0.0, 14.142135623730951)), ((7.0710678118654755, 14.142135623730951), (4.1421356237309501, 17.071067811865476)), ((4.1421356237309501, 17.071067811865476), (14.142135623730951, 27.071067811865476)), ((14.142135623730951, 27.071067811865476), (14.142135623730951, 23.535533905932738)), ((14.142135623730951, 23.535533905932738), (24.142135623730951, 23.535533905932738)), ((24.142135623730951, 3.5355339059327378), (14.142135623730951, 3.5355339059327378))])
        squ = shape('square.txt')
        #assert(not fitshapes2([squ], s))
    

if __name__ == '__main__':
    unittest.main()
        
